package main

import (
	"fmt"
)

const (
	SUITSIZE int = 4  //四种花色
	CARDRANK int = 13 //2 3 4....K A
)

type CardFlower int32

const (
	CardFlower_flowerHEITAO   CardFlower = 0
	CardFlower_flowerHONGTAO  CardFlower = 1
	CardFlower_flowerMEIHUA   CardFlower = 2
	CardFlower_flowerFANGKUAI CardFlower = 3
)

type Card struct {
	//Index int
	Suit  int //Suit=Index/13，程序统一标准：0是黑桃、1是红桃、2是梅花、3是方片
	Value int //Value=Index-Suit*13，0:2, 1:3, 11:K, 12:A， 1用A来填充//

	Showtime int //just for sort，牌值出现的次数//
}

func (this *Card) Print(){
	strSuits := []string{"♠", "♥", "♣", "♦"}
	strValues := []string{ "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}
	strValue := strValues[this.Value]
	fmt.Println(strSuits[this.Suit] + strValue)
}

func (this *Card) GetIndex() int {
	return this.Suit*CARDRANK + this.Value
}

func (this *Card) Reset() {
	//this.Index = 0
	this.Suit = 0
	this.Value = 0
	this.Showtime = 0
}

func (this *Card) Copy() *Card {
	newCard := &Card{
		Suit:     this.Suit,
		Value:    this.Value,
		Showtime: this.Showtime,
	}
	return newCard
}

func CardEqual(card1 *Card, card2 *Card) bool {
	if card1.Suit == card2.Suit && card1.Value == card2.Value {
		return true
	}
	return false
}

/*
这里是原来的Card的设计方式，为了兼容客户端，数据分析，新的Index需要转换//
type Card struct {
	Index  int
	Colour protobuf.CardFlower
	Value  int //0-12，0是A，12是K//
}
*/
func (this *Card) GetOldIndex() int {
	oldValue := this.Value + 1 //新k=11，老k=12，新2=0，老2=1
	if this.Value == 12 {
		oldValue = 0
	}
	return this.Suit*13 + oldValue
}

//实现sort包中的排序接口
type Cards []*Card

func (c Cards) Len() int {
	return len(c)
}

//排序，1，出现次数多的，2，值大的，3，花色大的，方梅红黑
func (c Cards) Less(i, j int) bool {
	if c[i].Showtime > c[j].Showtime {
		return true
	} else if c[i].Showtime < c[j].Showtime {
		return false
	} else if c[i].Value > c[j].Value {
		return true
		//return c[i].Value > c[j].Value
	} else if c[i].Value < c[j].Value {
		return false
	} else {
		return c[i].Suit < c[j].Suit
	}
}

func (c Cards) Swap(i, j int) {
	tmp := c[i]
	c[i] = c[j]
	c[j] = tmp
}

//根据花色和值生成一张牌//
func CreateCardBySuit(suit CardFlower, value int) *Card {
	c := new(Card)
	c.Suit = int(suit)
	c.Value = value
	//c.Index = c.Suit*CARDRANK + c.Value
	return c
}

//根据下标创建牌//
func CreateCardBuIndex(index int) *Card {
	c := new(Card)
	c.Suit = index / CARDRANK
	c.Value = index - c.Suit * CARDRANK
	//c.Index = index
	return c
}

func main() {
	//card := CreateCardBySuit(0, 0)
	card := CreateCardBuIndex(12)
	card.Print()
}